package com.topsoft.email.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.mvc.core.utils.UUIDGenerator;
import com.topsoft.email.model.EmailLog;
import com.topsoft.email.model.UserInfo;
import com.topsoft.email.service.IEmailSenderService;
import com.topsoft.email.service.IUserInfoService;
import com.topsoft.email.service.rabbitmq.MQProducer;
import com.topsoft.email.service.webservice.IEmailLogService;



@Controller
public class IndexAction {
	
	@Autowired
	public MQProducer mQProducer;
	
	@Autowired
    private RabbitTemplate amqpTemplate;
	
	@Autowired
    private RedisTemplate<String, String> redisTemplate;
	
	
	@Autowired
	public IUserInfoService userInfoService;
	
	@Autowired
	public IEmailSenderService emailSenderService;
	
	
	
	
	

	
	@RequestMapping(value = "/index.action",method={ RequestMethod.GET })
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
		for(int i=0;i<10;i++){
			amqpTemplate.convertAndSend("fanout_exchange", "queue_key", i);
		}
		return new ModelAndView("index");
	}
	
	
	@RequestMapping(value = "/content1.action",method={ RequestMethod.GET })
	public ModelAndView content(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
		
		return new ModelAndView("content1");
	}
	
	@RequestMapping(value = "/content2.action",method={ RequestMethod.GET })
	public ModelAndView content2(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
		
		return new ModelAndView("content2");
	}
	
	@RequestMapping(value = "/content3.action",method={ RequestMethod.GET })
	public ModelAndView content3(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
		
		return new ModelAndView("content3");
	}
	
	
	
	@RequestMapping(value = "/redisTest.action",method={ RequestMethod.GET })
	public ModelAndView redisTest(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
		for(int i=0;i<10;i++){
			redisTemplate.opsForSet().add(i+"", "-----------"+i);
		}
		return new ModelAndView("index");
	}
	
	
	@RequestMapping(value = "/userTest.action",method={ RequestMethod.GET })
	public ModelAndView userTest(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
		UserInfo user=userInfoService.load("1");
		System.out.println(user.getUserName());
		return new ModelAndView("index");
	}
	
	
	
	@RequestMapping(value = "/saveLog.action",method={ RequestMethod.GET })
	public ModelAndView saveLog(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
		
		EmailLog  log=new EmailLog();
		log.setReceiver("123");
		log.setSender("321");
		log.setId(UUIDGenerator.nextId());
		// emailSenderService.saveLog(log);
		return new ModelAndView("index");
	}

}
